<template>
  <div class="main-content noBorder">

    <el-tabs type="border-card">

      <!--  微信联合登录-->
      <el-tab-pane>
        <template #label>
          <el-icon>
            <User/>
          </el-icon>
          <span style="margin-left: 2px">联合登录</span>
        </template>

        <el-form ref="formDataRef" size="default" :model="formData" :rules="formRules" label-width="100px">

          <el-input v-model="formData.wx_app_id" style="width: 400px" placeholder="请输入" class="pd16" clearable>
            <template #prepend>appid</template>
          </el-input>

          <el-input v-model="formData.wx_app_secret" style="width: 500px" placeholder="请输入" clearable>
            <template #prepend>appid秘钥</template>
          </el-input>
          <br>
          <el-input v-model="formData.wx_mp_app_id" style="width: 400px" placeholder="请输入" class="pd16" clearable>
            <template #prepend>小程序appid</template>
          </el-input>

          <el-input v-model="formData.wx_mp_app_secret" style="width: 500px" placeholder="请输入" clearable>
            <template #prepend>小程序appid秘钥</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_h5_app_id" style="width: 400px" placeholder="请输入" class="pd16" clearable>
            <template #prepend>网站appid</template>
          </el-input>

          <el-input v-model="formData.wx_h5_app_secret" style="width: 500px" placeholder="请输入" clearable>
            <template #prepend>网站appid密码</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_service_app_id" style="width: 400px" placeholder="请输入" class="pd16"
                    clearable>
            <template #prepend>服务号appid</template>
          </el-input>

          <el-input v-model="formData.wx_service_app_secret" style="width: 500px" placeholder="请输入" clearable>
            <template #prepend>服务号appid秘钥</template>
          </el-input>
        </el-form>

      </el-tab-pane>

      <!--  微信支付-->
      <el-tab-pane>
        <template #label>
          <el-icon>
            <Money/>
          </el-icon>
          <span style="margin-left: 2px">支付参数</span>
        </template>

        <el-form ref="formDataRef" size="default" :model="formData" :rules="formRules" label-width="100px">

          <el-input v-model="formData.wx_pay_mch_id" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>mch_id</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_pay_api_key3" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>api_key3</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_pay_api_client_cert_p12" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>api_client_cert.p12</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_pay_api_client_cert_pem" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>api_client_cert.pem</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_pay_api_client_key_pem" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>api_client_key.pem</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_pay_serial_no" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>serial_no</template>
          </el-input>
          <br>

          <el-input v-model="formData.wx_pay_plat_serial_no" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>plat_serial_no</template>
          </el-input>
        </el-form>

      </el-tab-pane>

      <!--  地址-->
      <el-tab-pane>
        <template #label>
          <el-icon>
            <el-icon>
              <ChromeFilled/>
            </el-icon>
          </el-icon>
          <span style="margin-left: 2px">URL地址</span>
        </template>
        <el-form ref="formDataRef" size="default" :model="formData" :rules="formRules" label-width="100px">

          <el-input v-model="formData.wx_pay_call_back_url" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>支付回调地址</template>
          </el-input>

          <el-input v-model="formData.wx_pay_refund_back_url" style="width: 600px" placeholder="请输入" class="pd8">
            <template #prepend>退款回调地址</template>
          </el-input>

        </el-form>

      </el-tab-pane>

    </el-tabs>


    <div class="fx_center_center pd16">
      <el-button type="primary" @click="submitEdit" style="width: 200px" size="large">保 存</el-button>
    </div>

  </div>

</template>

<script setup>
import {getCurrentInstance, onMounted, ref} from "vue";
import $modal from "@/common/utils/modal";
import {selectSetting, updateSetting} from "@/common/api/system";

const {proxy} = getCurrentInstance();

let optionKey = {
  loading: false,
}

let formKey = {
  wx_app_id: "",
  wx_app_secret: "",
  wx_mp_app_id: "",
  wx_mp_app_secret: "",
  wx_h5_app_id: "",
  wx_h5_app_secret: "",
  wx_service_app_id: "",
  wx_service_app_secret: "",
  wx_pay_mch_id: "",
  wx_pay_api_key3: "",
  wx_pay_api_client_cert_p12: "",
  wx_pay_api_client_cert_pem: "",
  wx_pay_api_client_key_pem: "",
  wx_pay_serial_no: "",
  wx_pay_plat_serial_no: "",
  wx_pay_call_back_url: "",
  wx_pay_refund_back_url: "",
}

const formRules = ref({
  dictName: [{required: true, message: "不能为空", trigger: "blur"}],
  dictValue: [{required: true, message: "不能为空", trigger: "blur"}],
  dictKey: [{required: true, message: "不能为空", trigger: "blur"}],
});


const option = ref(optionKey);

const formData = ref(formKey);


//页面加载完成回调
onMounted(() => {
  searchData()
})

//搜索数据
function searchData() {
  let keys = Object.keys(formData.value).join(",")

  selectSetting({keys}).then(res => {
    if (res.success) {
      formData.value = res.data
    }
  })

}

function submitEdit() {
  //console.log("formData.value  ", formData.value)
  let content = `正在修改配置信息，请确认是否继续操作？ `
  $modal.confirmWarning(content).then(() => {
    proxy.$refs["formDataRef"].validate(valid => {
      if (valid) {
        updateSetting(formData.value).then(res => {
          if (res.success) {
            $modal.success();
          }
        })
      }
    });
  }).catch(() => $modal.cancel());
}
</script>

<style scoped lang="less">

</style>